package es.udc.pfc.davidparedes.web.pages.user;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.PageRenderLinkSource;

import es.udc.pfc.davidparedes.model.storeservice.StoreService;
import es.udc.pfc.davidparedes.model.taskservice.TaskService;
import es.udc.pfc.davidparedes.model.userprofile.UserProfile;
import es.udc.pfc.davidparedes.model.userservice.UserService;
import es.udc.pfc.davidparedes.model.util.GlobalNames;
import es.udc.pfc.davidparedes.model.util.InstanceNotFoundException;
import es.udc.pfc.davidparedes.model.vendor.Vendor;

import es.udc.pfc.davidparedes.web.services.AuthenticationPolicy;
import es.udc.pfc.davidparedes.web.services.AuthenticationPolicyType;
import es.udc.pfc.davidparedes.web.util.UserSession;

@AuthenticationPolicy(AuthenticationPolicyType.MANAGER_OR_ABOVE)
public class AddMissionToVendor {
    
    @SessionState(create=false)
    private UserSession userSession;
    
    @Property
	private UserProfile userProfile;
    
    @Inject
    private UserService userService;
    
    @Inject
    private TaskService taskService;
    
    @Inject
    private StoreService storeService;

    @Property
    private Date startDate;
    
    @Property
    private Date endDate;
    
    @Property
    private String startTime;
    
    @Property
    private String endTime;
    
    @Property
    private Boolean isRange;
    
    @Property
    private Long vendorId;

    @Property
    private String name;
    
    @Property
    private String missionType;
    
    @Property
    private List<String> missionTypes;
    
    @Component
    private Form addMissionForm;
    
    @Inject 
    private PageRenderLinkSource pageLinkSource;
    
    @Inject
    private Messages messages;
    
    
    void onActivate(Long id) {
    	try {
    		userService.findVendor(id);
    		vendorId = id;
    		
    		userProfile = userService.findUserProfile(userSession.getUserProfileId());
    		
    		missionTypes = new ArrayList<String>();
    		missionTypes.add(Character.toString(GlobalNames.TASK_NORMAL));
    		missionTypes.add(Character.toString(GlobalNames.TASK_VACATION));
    		missionTypes.add(Character.toString(GlobalNames.TASK_NIGHT));
    		missionTypes.add(Character.toString(GlobalNames.TASK_LOW));
    		missionTypes.add(Character.toString(GlobalNames.TASK_FESTIVE));
    		missionTypes.add(Character.toString(GlobalNames.TASK_ABSENTEEISM));
    		missionTypes.add(Character.toString(GlobalNames.TASK_COMPLEMENTARY_NORMAL));
    		missionTypes.add(Character.toString(GlobalNames.TASK_COMPLEMENTARY_FESTIVE));
    		missionTypes.add(Character.toString(GlobalNames.TASK_COMPLEMENTARY_NIGHT));
    		
			} catch (InstanceNotFoundException e) {
		}
    }

    Object onValidateFromAddMissionForm() {

        if (!addMissionForm.isValid()) {
            return this;
        }
        
        Vendor vendor;
		try {
			vendor = userService.findVendor(vendorId);
		
        
        String[] missionStartTime = startTime.split(":");
        Integer missionStartHour = Integer.parseInt(missionStartTime[0]);
        Integer missionStartMin = Integer.parseInt(missionStartTime[1]);
        Calendar missionStart = Calendar.getInstance();
        missionStart.setTime(startDate);
        if ((0 <= missionStartHour) && (missionStartHour <= 23) && (0 <= missionStartMin) && (missionStartMin <= 59)) {
	        missionStart.set(Calendar.HOUR_OF_DAY, missionStartHour);
	        missionStart.set(Calendar.MINUTE, missionStartMin);
        } else {
        	return this;
        }

        String[] missionEndTime = endTime.split(":");
        Integer missionEndHour = Integer.parseInt(missionEndTime[0]);
        Integer missionEndMin = Integer.parseInt(missionEndTime[1]);
        Calendar missionEnd = Calendar.getInstance();
        missionEnd.setTime(endDate);
        if ((0 <= missionEndHour) && (missionEndHour <= 23) && (0 <= missionEndMin) && (missionEndMin <= 59)) {
	        missionEnd.set(Calendar.HOUR_OF_DAY, missionEndHour);
	        missionEnd.set(Calendar.MINUTE, missionEndMin);
        } else {
        	return this;
        }

        if (missionStart.before(missionEnd)) {
        	try {
        		
        		if (isRange) {
        			taskService.addMissionsRange(name, missionStart, missionEnd, (char) missionType.charAt(0), vendor.getUserProfileId(), vendor.getStore().getStoreId());
        		} else {
        			taskService.addMission(name, missionStart, missionEnd, (char) missionType.charAt(0), vendor.getUserProfileId(), vendor.getStore().getStoreId());
        		}
			} catch (InstanceNotFoundException e) {
				addMissionForm.recordError(messages.get("error-notFound"));
			}
        }
        
        return pageLinkSource.createPageRenderLinkWithContext("user/DisplayVendorSchedule", vendor.getUserProfileId());
        
		} catch (InstanceNotFoundException e1) {
			addMissionForm.recordError(messages.get("error-notFound"));
		}
		
		return this;
        
    }

}
